﻿using System.Xml.Linq;
using FirstFloor.Documents.Annotations;
using FirstFloor.Documents.Annotations.Storage;
using FirstFloor.Documents.Presentation.Controls;

namespace FirstFloor.Documents.Presentation.Annotations
{
    /// <summary>
    /// Represents a sticky note annotation
    /// </summary>
    public class StickyNote
        : Annotation
    {
        /// <summary>
        /// Identifies the sticky note annotation type.
        /// </summary>
        public static readonly XName TypeStickyNote = XName.Get("StickyNote", AnnotationStore.NamespaceAnnotations);

        private double left, top;
        private string text = "Enter your text here...";

        /// <summary>
        /// Initializes a new instance of the <see cref="StickyNote"/> class.
        /// </summary>
        public StickyNote()
            : base(TypeStickyNote)
        {
        }

        /// <summary>
        /// Gets or sets the page number associated with this sticky note instance.
        /// </summary>
        /// <value>The page number.</value>
        public int PageNumber { get; set; }

        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get { return this.text; }
            set
            {
                if (this.text != value) {
                    this.text = value;
                    OnPropertyChanged("Text");
                }
            }
        }

        /// <summary>
        /// Gets or sets the distance between left corner of the page and the left corner of this sticky note.
        /// </summary>
        /// <value>The left.</value>
        public double Left
        {
            get { return this.left; }
            set
            {
                if (this.left != value) {
                    this.left = value;
                    OnPropertyChanged("Left");
                }
            }
        }

        /// <summary>
        /// Gets or sets the distance between top of the page and the top of this sticky note.
        /// </summary>
        /// <value>The top.</value>
        public double Top
        {
            get { return this.top; }
            set
            {
                if (this.top != value) {
                    this.top = value;
                    OnPropertyChanged("Top");
                }
            }
        }

        /// <summary>
        /// Determines whether the current annotation instance includes specified page.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <returns>
        /// 	<c>true</c> if the specified page is included; otherwise, <c>false</c>.
        /// </returns>
        public override bool ContainsPage(int pageNumber)
        {
            return this.PageNumber == pageNumber;
        }

        /// <summary>
        /// Renders the annotation.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void RenderAnnotation(AnnotationRenderContext context)
        {
            // use the top-level annotation adorner layer to render sticky notes. sticky notes
            // always overlap all other annotation and selection visuals
            var adornerLayer = context.AnnotationAdornerLayer;

            // create a new sticky note visual
            var visual = new StickyNoteControl() {
                AnnotationStore = context.AnnotationStore,
                StickyNote = this
            };

            adornerLayer.Children.Add(visual);
        }
    }
}
